//
//  HJZCommentTableViewCell.m
//  HJZWeiBo
//
//  Created by 蒋晨成 on 16/6/3.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "HJZCommentTableViewCell.h"
#import "RCLabel.h"
#import "HtmlString.h"
#import "HJZComment.h"
#import <UIImageView+WebCache.h>
@interface HJZCommentTableViewCell ()<RCLabelDelegate>

@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UIButton *nameBtn;
@property (nonatomic, strong) UIImageView *vipImageView;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) RCLabel *contentLabel;
@property (nonatomic, strong) UIImageView *line;

@end

@implementation HJZCommentTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *ID = @"comment";
    HJZCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[HJZCommentTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setCommentFrame:(HJZCommentFrame *)commentFrame {
    _commentFrame = commentFrame;
    HJZComment *comment = commentFrame.comment;
    HJZUser *user = comment.user;
  
    // 头像
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:user.profile_image_url]
                     placeholderImage:[UIImage imageNamed:@"avatar_default_small"]];
    self.iconImageView.frame = commentFrame.iconViewF;
    
    // 昵称
    [self.nameBtn setTitle:comment.user.name forState:UIControlStateNormal];
    self.nameBtn.frame = commentFrame.nameBtnF;
    
    // vip
    if (user.mbtype > 0) {
        self.vipImageView.hidden = NO;
        NSString *name = [NSString stringWithFormat:@"common_icon_membership_level%d", user.mbrank];
        self.vipImageView.image = [UIImage imageNamed:name];
        self.vipImageView.frame = commentFrame.vipViewF;
        
        [self.nameBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    } else {
        self.vipImageView.hidden = YES;
        
        [self.nameBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    // 时间
    self.timeLabel.text = comment.created_at;
    self.timeLabel.frame = commentFrame.timeLabelF;
    
    NSString *commentString = nil;
    if (comment.text) {
        commentString = comment.text;
    }else {
        commentString = comment.reply_comment.text;
    }
 
    
    // 正文
    NSString *str = [HtmlString transformString:commentString];
    self.contentLabel.componentsAndPlainText = [RCLabel extractTextStyle:str];
    self.contentLabel.frame = commentFrame.contentLabelF;
    
    self.line.frame = CGRectMake(CGRectGetMaxX(_iconImageView.frame), CGRectGetMaxY(_commentFrame.contentLabelF) + 10, kWidth - CGRectGetMaxX(_iconImageView.frame), 1);
    
}

- (void)setupView {
    // 1. 头像
    self.iconImageView = [[UIImageView alloc] init];
    [self addSubview:_iconImageView];
  
    
    // 2. 昵称
    self.nameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nameBtn.titleLabel.font = [UIFont systemFontOfSize:HJZStatusNameFont];
    [self addSubview:_nameBtn];
 
    
    // 3. 会员图标
    self.vipImageView = [[UIImageView alloc] init];
    self.vipImageView.contentMode = UIViewContentModeCenter;
    [self addSubview:_vipImageView];
   
    
    // 4. 时间
    self.timeLabel = [[UILabel alloc] init];
    self.timeLabel.font = [UIFont systemFontOfSize:HJZStatusTimeFont];
    self.timeLabel.textColor = COLOR(135, 135, 135, 1);
    [self addSubview:_timeLabel];
    
    self.contentLabel = [[RCLabel alloc] init];
    self.contentLabel.delegate = self;
    self.contentLabel.font = [UIFont systemFontOfSize:HJZStatusContentFont];
    self.contentLabel.textColor = COLOR(35, 35, 35, 1);
    self.contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [self addSubview:_contentLabel];

    
    self.line = [[UIImageView alloc] init];
    self.line.backgroundColor = PKCOLOR(230, 230, 230);
    [self.contentView addSubview:_line];

}
- (void)RCLabel:(id)RCLabel didSelectLinkWithURL:(NSString *)url {
    NSLog(@"%@",url);
}

@end
